<template>
  <a-spin :spinning="spinning">
    <page-header-wrapper>
      <application-search
        :filterFields="['SubmitToCity']"
        :moreHandles="[{ name: '批量接收', data: selectedRowKeys }]"
        @search="handleSearch"
        @reset="handleReset"
      ></application-search>
      <a-card :bordered="false">
        <s-table
          ref="table"
          size="default"
          rowKey="ID"
          :row-selection="{ fixed: true, selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
          :columns="columns"
          :data="loadData"
          :scroll="{ x: 2700 }"
          :pagination="{ showTotal: (total) => `共 ${total} 条` }"
        >
          <div slot="tabAction">
            <buttonSelect
              class="m-l-10"
              v-model="typeSelect"
              :options="[
                { value: '', label: '全部' },
                { value: '1', label: '未接收' },
                { value: '2', label: '已接收' },
              ]"
              @change="handleButtonSelectChange"
            ></buttonSelect>
          </div>
          <span slot="index" slot-scope="text, record, index">{{ index + 1 }}</span>
          <span slot="VerifyResult" slot-scope="text" :class="textColor(text)">{{ text }}</span>
          <span slot="SubmitToDate" slot-scope="text">{{ text | moment }}</span>
          <span slot="ProcessUser" slot-scope="text">{{ text || '/' }}</span>
          <span slot="ProcessDate" slot-scope="text">{{ text | moment }}</span>
          <span slot="description" slot-scope="text">
            <ellipsis :length="16">{{ text }}</ellipsis>
          </span>
          <y-button
            slot="action"
            slot-scope="text, record"
            type="link"
            :disabled="record.SubmitToCity === '2'"
            @click="handleReceive(record)"
            >{{ record.SubmitToCity === '2' ? '已接收' : '接收' }}</y-button
          >
        </s-table>
      </a-card>
    </page-header-wrapper>
  </a-spin>
</template>

<script>
import { STable, Ellipsis, buttonSelect, applicationSearch } from '@/components'
import { getProposerList, postProposerExamine } from '@/api/common'
import { postProposerExamineBatch } from '@/api/cityReviewAndManage'
import moment from 'moment'
export default {
  name: 'cityComplexInquireList',
  components: {
    STable,
    Ellipsis,
    buttonSelect,
    applicationSearch,
  },
  computed: {
    textColor() {
      return function (text) {
        const classMap = new Map([
          ['符合', 'text-success'],
          ['不符合', 'text-error'],
          ['未审核', 'text-warning'],
        ])
        return classMap.get(text)
      }
    },
  },
  data() {
    return {
      typeSelect: '',
      spinning: false, // 加载
      queryParam: {
        UserID: this.$store.getters.userid,
        Mode: 'RegionReceive',
        strContent: {},
      },
      columns: [
        {
          title: '序号',
          align: 'center',
          width: '80px',
          scopedSlots: { customRender: 'index' },
        },
        {
          title: '申请编号',
          align: 'center',
          dataIndex: 'ApplyNo',
        },
        {
          title: '申请人',
          align: 'center',
          dataIndex: 'Proposer',
        },
        {
          title: '身份证号',
          align: 'center',
          dataIndex: 'IDCard',
        },
        {
          title: '保障方式',
          align: 'center',
          customRender: (text, record) => record.ApplyClass,
          // dataIndex: 'ApplyClass'
        },
        {
          title: '区级审核状态',
          align: 'center',
          fixed: 'right',
          dataIndex: 'VerifyResult',
          scopedSlots: { customRender: 'VerifyResult' },
        },
        {
          title: '核定保障人数',
          align: 'center',
          dataIndex: 'CityVfyNumber',
        },
        {
          title: '保障类型',
          align: 'center',
          customRender: (text, record) => record.ApplyClass,
        },
        {
          title: '配租方式',
          align: 'center',
          dataIndex: 'CityVfyAssign',
        },
        {
          title: '区县',
          align: 'center',
          dataIndex: 'RegionName',
          scopedSlots: { customRender: 'description' },
        },
        {
          title: '街道办事处',
          align: 'center',
          dataIndex: 'TownshipName',
        },
        {
          title: '上报人',
          align: 'center',
          dataIndex: 'SubmitToUser',
        },
        {
          title: '上报日期',
          align: 'center',
          dataIndex: 'SubmitToDate',
          scopedSlots: { customRender: 'SubmitToDate' },
        },
        {
          title: '接收人',
          align: 'center',
          dataIndex: 'ProcessUser',
          scopedSlots: { customRender: 'ProcessUser' },
        },
        {
          title: '接收日期',
          align: 'center',
          dataIndex: 'ProcessDate',
          scopedSlots: { customRender: 'ProcessDate' },
        },
        {
          title: '接收',
          align: 'center',
          width: '80px',
          fixed: 'right',
          scopedSlots: { customRender: 'action' },
        },
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: (parameter) => {
        const requestParameters = Object.assign({}, parameter, this.queryParam)
        requestParameters.strContent.SubmitToCity = this.typeSelect
        return getProposerList(requestParameters).then((res) => {
          return res
        })
      },
      selectedRowKeys: [],
    }
  },
  methods: {
    onSelectChange(selectedRowKeys) {
      this.selectedRowKeys = selectedRowKeys
    },
    handleSearch(values) {
      this.queryParam.strContent = values
      this.$refs.table.refresh(true)
    },
    handleReset() {
      this.queryParam.strContent = {}
      this.typeSelect = ''
      this.$refs.table.refresh(true)
    },
    handleButtonSelectChange(e) {
      this.$refs.table.refresh(true)
    },
    handleBatchReceive() {
      if (this.$isEmpty(this.selectedRowKeys)) {
        this.$message.warning('至少选择一条用户信息')
      } else {
        this.$confirm({
          title: '提示',
          content: `确定接收所选的${this.selectedRowKeys.length}条用户信息吗？`,
          onOk: () => {
            return postProposerExamineBatch({
              State: 'CityReceive',
              ProposerModules: {
                ID: this.selectedRowKeys.join(','),
                ProcessUser: this.$store.getters.nickname,
                ProcessDate: moment().format('YYYY-MM-DD HH:mm:ss'),
                LastAlterDate: moment().format('YYYY-MM-DD HH:mm:ss'),
                LastAlterUserID: this.$store.getters.userid,
              },
            }).then((res) => {
              this.$message.success('接收成功')
              this.$refs.table.refresh()
              this.selectedRowKeys = []
            })
          },
        })
      }
    },
    handleReceive(record) {
      this.$confirm({
        title: '提示',
        content: '是否接收该用户信息？',
        onOk: () => {
          return postProposerExamine({
            State: 'CityReceive',
            ProposerModules: {
              ID: record.ID,
              ProcessUser: this.$store.getters.nickname,
              ProcessDate: moment().format('YYYY-MM-DD HH:mm:ss'),
              LastAlterDate: moment().format('YYYY-MM-DD HH:mm:ss'),
              LastAlterUserID: this.$store.getters.userid,
            },
          }).then((res) => {
            this.selectedRowKeys = []
            this.$message.success('接收成功')
            this.$refs.table.refresh()
          })
        },
      })
    },
  },
}
</script>
<style lang="less" scoped></style>
